package com.gambi.tapestry.client.gwtui.widgets;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;

public class LoginWidget extends Composite {

	private HorizontalPanel horizontalPanel = new HorizontalPanel();
	private RequestBuilder builder;
	private TextBox userid = new TextBox();
	PasswordTextBox passwd = new PasswordTextBox();

	public LoginWidget(String id, String div, String url) {
		builder = new RequestBuilder(RequestBuilder.POST, url);
		builder.setHeader("Content-Type", "text/gwt");
		builder.setCallback(new RequestCallback() {
			public void onError(Request request, Throwable exception) {
				displayError("Couldn't retrieve JSON");
			}

			public void onResponseReceived(Request request, Response response) {
				if (200 == response.getStatusCode()) {
					processResponse(response);
				} else {
					displayError("Couldn't retrieve JSON ("
							+ response.getStatusText() + ")");
				}
			}
		});

		// Create the UI
		horizontalPanel.add(new Label("User ID"));
		userid.setName("userid");
		horizontalPanel.add(userid);

		horizontalPanel.add(new Label("Password"));
		passwd.setName("passwd");
		horizontalPanel.add(passwd);

		Button submit = new Button("Submit");
		submit.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				GWT.log("Submit");
				submitLogin();
			}
		});

		horizontalPanel.add(submit);

		// Assemble Main panel.
		errorMsgLabel.setStyleName("errorMessage");
		errorMsgLabel.setVisible(false);

		horizontalPanel.add(errorMsgLabel);
		initWidget(horizontalPanel);
	}

	private void submitLogin() {
		try {
			builder.setRequestData("{\"userid\":\"" + userid.getText()
					+ "\", \"password\":\"" + passwd.getText() + "\"}");
			builder.send();
		} catch (RequestException e) {
			displayError("Couldn't retrieve JSON");
		}
	}

	private Label errorMsgLabel = new Label();

	private void processResponse(Response response) {
		// This is a commonn case with Tapestry
		JSONObject responseJsonObject = (JSONObject) JSONParser
				.parseStrict(response.getText());
		if (responseJsonObject.containsKey("redirectURL")) {
			Window.Location.assign(((JSONString) responseJsonObject
					.get("redirectURL")).stringValue());
		} else {
			// Clear any errors.
			errorMsgLabel.setVisible(false);
			errorMsgLabel.setText("Response: " + response);
			errorMsgLabel.setVisible(true);
		}
	}

	private void displayError(String error) {
		errorMsgLabel.setVisible(false);
		errorMsgLabel.setText("Error: " + error);
		errorMsgLabel.setVisible(true);
	}
}
